import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
import { getWeather } from "./wetherHandler.js";

const server = new McpServer({
  name: "wether-mcp-server",
  version: "1.0.0",
  description: "一个提供天气查询的MCP服务器",
});

server.registerTool(
  "getWether",
  {
    title: "查询天气",
    description: "查询指定城市和日期的天气情况",
    inputSchema: {
      city: z.string().describe("城市名，例如：成都、上海、北京"),
      date: z.string().describe("日期的描述，例如 今天、明天、2025-7-20"),
    },
  },
  async ({ city, date }) => {
    // 具体工具的执行逻辑
    try {
      const result = await getWeather(city, date);
      return {
        content: [
          {
            type: "text",
            text: result,
          },
        ],
      };
    } catch (err) {
      console.error(err);
      return {
        content: [
          {
            type: "text",
            text: err.message,
          },
        ],
      };
    }
  }
);

const transport = new StdioServerTransport(); // 创建一个 stdio 的传输通道
server.connect(transport);
